import Vue from "vue";
import { getAction } from '@/api/manage';
import store from '@/store/'
import { ACCESS_TOKEN, USER_NAME,USER_INFO,USER_AUTH,SYS_BUTTON_AUTH,UI_CACHE_DB_DICT_DATA,TENANT_ID,CACHE_INCLUDED_ROUTES,MERCHANT_TYPE } from "@/store/mutation-types"
import { welcome } from "@/utils/util"
// 授权免登陆
export function authorizeLogin(parma) {
    return new Promise((resolve, reject) => {
        getAction('/sys/exemptionLogin', { data:decodeURI(parma)}).then(res => {
          if(res.success){
            const result = res.result
            const userInfo = result.userInfo  
            Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(UI_CACHE_DB_DICT_DATA, result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(MERCHANT_TYPE, userInfo.merchantType, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set('canLogin', true)
            store.commit('SET_TOKEN', result.token)
            store.commit('SET_INFO', userInfo)
            store.commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname, welcome: welcome() })
            store.commit('SET_AVATAR', userInfo.avatar)
            resolve(result.token)
          }else{
            reject(res.message)
          }
        }).catch(err=>{
          reject('接口报错')
         }) 
    })
}
